package com.pimee.web.sys;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysUser;
import com.pimee.model.SysUserRole;
import com.pimee.model.vo.SysUserVo;
import com.pimee.service.admin.ISysPostService;
import com.pimee.service.admin.ISysRoleService;
import com.pimee.service.admin.ISysUserService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping(value = "admin/sys/user")
public class SysUserController extends AbstractController {
	
	private String prefix = "admin/sys/user";

	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysRoleService sysRoleService;
	@Autowired
	private ISysPostService sysPostService;

	@ApiOperation("进入新增用户页面")
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String userAdd(ModelMap mmap) {
		mmap.put("roles", sysRoleService.listAll());
		mmap.put("posts", sysPostService.listAll());
		return prefix + "/add";
	}

	@ApiOperation("进入更新用户页面")
	@RequestMapping(value = "/edit/{userId}", method = RequestMethod.GET)
	public String userUpdate(ModelMap mmap, @PathVariable("userId") Long userId) {
		mmap.put("user", sysUserService.selectUserById(userId));
		mmap.put("roles", sysRoleService.selectRolesByUserId(userId));
		mmap.put("posts", sysPostService.selectPostsByUserId(userId));
		return prefix + "/edit";
	}

	@ApiOperation("查询用户列表")
	@RequiresPermissions("system:user:list")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public Object listUser(@RequestParam Map<String, Object> params, ModelMap modelMap) {
		PageInfo<SysUserVo> pageUser = sysUserService.pageUser(params);
		return setSuccessModelMap(modelMap, pageUser);
	}

	@ApiOperation("校验用户名唯一")
	@PostMapping("/checkLoginNameUnique")
	@ResponseBody
	public String checkLoginNameUnique(SysUser sysUser) {
		return sysUserService.checkLoginNameUnique(sysUser.getLoginName());
	}

	@ApiOperation("校验手机号码唯一")
	@PostMapping("/checkPhoneUnique")
	@ResponseBody
	public String checkPhoneUnique(SysUser sysUser) {
		return sysUserService.checkPhoneUnique(sysUser.getPhonenumber());
	}

	@ApiOperation("保存新增用户信息")
	@RequiresPermissions("system:user:add")
	@PostMapping("/add")
	@ResponseBody
	public Object addUser(@Validated SysUser sysUser, ModelMap modelMap) {
		sysUserService.addUser(sysUser);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("保存修改用户信息")
	@RequiresPermissions("system:user:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Object editUser(@Validated SysUser sysUser, ModelMap modelMap) {
		sysUser.setUpdateBy(ShiroUtils.getLoginName());
		sysUserService.updateUser(sysUser);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("用户状态修改")
	@RequiresPermissions("system:user:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public Object changeStatus(SysUser sysUser, ModelMap modelMap) {
		sysUserService.changeStatus(sysUser);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("用户分配角色")
	@RequiresPermissions("system:user:edit")
	@PostMapping("/update/role")
	@ResponseBody
	public Object updateUserRole(Long userId, @RequestParam("roleIds[]") Long[] roleIds, ModelMap modelMap) {
		log.info("userId:{}, roleIds:{}", userId, roleIds);
		sysUserService.updateUserRole(userId, roleIds);
		return setSuccessModelMap(modelMap);
	}

	@ApiOperation("获取用户关联的角色")
	@RequiresPermissions("system:user:list")
	@ResponseBody
	@GetMapping(value = "/rela/listRole")
	public Object listRelaRole(@RequestParam Long userId, ModelMap modelMap) {
		List<SysUserRole> relaRoleList = sysUserService.listRealRole(userId);
		return setSuccessModelMap(modelMap, relaRoleList);
	}
	
	@ApiOperation("进入重置密码")
	@RequiresPermissions("system:user:resetPwd")
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Long userId, ModelMap modelMap) {
		modelMap.put("user", sysUserService.selectUserById(userId));
        return prefix + "/resetPwd";
    }

	@ApiOperation("重置密码")
    @RequiresPermissions("system:user:resetPwd")
    @PostMapping("/resetPwd")
    @ResponseBody
    public Object resetPwdSave(SysUser sysUser, ModelMap modelMap) {
		sysUser.setUpdateBy(ShiroUtils.getLoginName());
    	sysUserService.resetUserPwd(sysUser);
    	return setSuccessModelMap(modelMap);
    }

}
